// pages/index/welcome/welcome.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFlag: false,
    countDownNum: '3'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取是否已经选择过社区
    wx.getStorage({
      key: 'community',
      success: res => {
        console.error(JSON.stringify(res.data))
        if (res.data == 1) {
          app.globalData.appType = 1
        } else if (res.data == 2) {
          app.globalData.appType = 2
        } else if (res.data == 3) {
          app.globalData.appType = 3
        } else if (res.data == 4) {
          app.globalData.appType = 4
        } else if (res.data == 5) {
          app.globalData.appType = 5
        } else if (res.data == 6) {
          app.globalData.appType = 6
        }else if (res.data == 7) {
          app.globalData.appType = 7
        }else if (res.data == 8) {
          app.globalData.appType = 8
        } else {
          app.globalData.appType = 0
        }
      }
    })
    let than = this
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              app.globalData.wxUserInfo = res.userInfo
              than.setData({
                isFlag: true
              })
            }
          })
        }
      }
    })
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        });
      }
    });
    //this.welcomeLoad();


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.countDown();
    app.globalData.yardId = wx.getStorageSync("yardId");
    app.globalData.roomId = wx.getStorageSync("roomId");
    app.globalData.areaCode = wx.getStorageSync("areaCode");
    app.globalData.userInfo["user_id"] = wx.getStorageSync('user_id')?wx.getStorageSync('user_id'):'test';
    app.globalData.userInfo["people_id"] = wx.getStorageSync('people_id');
    app.globalData.userInfo["name"] = wx.getStorageSync('name');
    //获取个人信息
    if (!app.globalData.userInfo["user_id"]) {
      app.getUserInfo();
    }
    if (!app.globalData.roomId) {
      if (!app.globalData.userInfo["people_id"]) {
        return;
      }
      app.isBindCommunity();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 计时完成
   */
  welcomeCountdownComplate: function () {
    // wx.reLaunch({
    //   url: '/pages/mine/login/login',
    // })
    console.error(this.data.isFlag)
    if(this.data.isFlag){
      if (app.globalData.appType == 1) {
        app.globalData.appTitle = '向龙社区'
        app.globalData.unitCode = '86.001.001.001.001.004.005'
        app.globalData.parentAreaCode = '86.51.01.08.04.05'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 2) {
        app.globalData.appTitle = '华冠社区'
        app.globalData.unitCode = '86.001.001.001.001.004.006'
        app.globalData.parentAreaCode = '86.51.01.08.04.06'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 3) {
        app.globalData.appTitle = '同乐社区'
        app.globalData.unitCode = '86.001.001.001.001.004.008'
        app.globalData.parentAreaCode = '86.51.01.08.04.08'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 4) {
        app.globalData.appTitle = '隆兴路社区'
        app.globalData.unitCode = '86.001.001.001.001.004.001'
        app.globalData.parentAreaCode = '86.51.01.08.04.01'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 5) {
        app.globalData.appTitle = '华泰社区'
        app.globalData.unitCode = '86.001.001.001.001.004.009'
        app.globalData.parentAreaCode = '86.51.01.08.04.09'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 6) {
        app.globalData.appTitle = '桂林社区'
        app.globalData.unitCode = '86.001.001.001.001.004.003'
        app.globalData.parentAreaCode = '86.51.01.08.04.03'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 7) {
        app.globalData.appTitle = '龙祥社区'
        app.globalData.unitCode = '86.001.001.001.001.004.011'
        app.globalData.parentAreaCode = '86.51.01.08.04.11'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 8) {
        app.globalData.appTitle = '龙瑞社区'
        app.globalData.unitCode = '86.001.001.001.001.004.012'
        app.globalData.parentAreaCode = '86.51.01.08.04.12'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else {}
      return
    }
    wx.reLaunch({
      url: '/packageA/pages/select_community/select_community',
    })
  },
  /**
   * 跳过
   */
  welcomeDump: function () {
    // wx.reLaunch({
    //   url: '/pages/mine/login/login',
    // })
    console.error(this.data.isFlag)
    if(this.data.isFlag){
      console.error(app.globalData.appType)
      if (app.globalData.appType == 1) {
        app.globalData.appTitle = '向龙社区'
        app.globalData.unitCode = '86.001.001.001.001.004.005'
        app.globalData.parentAreaCode = '86.51.01.08.04.05'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 2) {
        app.globalData.appTitle = '鹤林社区'
        app.globalData.unitCode = '86.001.001.001.001.004.006'
        app.globalData.parentAreaCode = '86.51.01.08.04.06'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 3) {
        app.globalData.appTitle = '同乐社区'
        app.globalData.unitCode = '86.001.001.001.001.004.008'
        app.globalData.parentAreaCode = '86.51.01.08.04.08'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 4) {
        app.globalData.appTitle = '隆兴路社区'
        app.globalData.unitCode = '86.001.001.001.001.004.001'
        app.globalData.parentAreaCode = '86.51.01.08.04.01'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 5) {
        app.globalData.appTitle = '华泰社区'
        app.globalData.unitCode = '86.001.001.001.001.004.009'
        app.globalData.parentAreaCode = '86.51.01.08.04.09'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 6) {
        app.globalData.appTitle = '桂林社区'
        app.globalData.unitCode = '86.001.001.001.001.004.003'
        app.globalData.parentAreaCode = '86.51.01.08.04.03'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 7) {
        app.globalData.appTitle = '龙祥社区'
        app.globalData.unitCode = '86.001.001.001.001.004.011'
        app.globalData.parentAreaCode = '86.51.01.08.04.11'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else if (app.globalData.appType == 8) {
        app.globalData.appTitle = '龙瑞社区'
        app.globalData.unitCode = '86.001.001.001.001.004.012'
        app.globalData.parentAreaCode = '86.51.01.08.04.12'
        wx.switchTab({
          url: '/pages/index/index',
        })
        app.wxGetRequest('/applet/associateArea', {
          user_id: app.globalData.userInfo["user_id"],
          area_code: app.globalData.parentAreaCode
        })
      } else {}
      return
    }
    wx.reLaunch({
      url: '/packageA/pages/select_community/select_community',
    })
  },
  /**
   * 欢迎页
   */
  welcomeLoad: function () {
    app.wxGetRequest("/wechart/microofficial/getBackgroundImg", {
      area_code: app.globalData.parentAreaCode
    }).then(result => {
      let data = result.data.data;
      if (data) {
        this.setData({
          picture: data.img_url,
        });
      }
    });
  },
  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;
      that.data.timer = setInterval(function () {
        if (countDownNum == 0) {
          clearInterval(that.data.timer);
          that.welcomeCountdownComplate()
          
        }else{
        countDownNum--;
        that.setData({
          countDownNum: countDownNum
        })}
      }, 1000)
  },
})